package com.browsesoft.resource;

import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.browsesoft.ExtendElement;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
public class LicenseFormComponent extends HTMLBasicComponent {
	Element rightElement;

	public LicenseFormComponent(HTMLBasicComponent parent) throws Exception {
		super(parent);
	}

	/**
	 * 初始化时创建子组件并设置初始化参数
	 */
	public void init(HttpServletRequest request, Element e) throws Exception {
		super.init(request, e);
		rightElement = (Element) ExtendElement.findElements(this.getElement(),
				"righttypes", "righttypes").get(0);
		rightElement = (Element) rightElement.cloneNode(true);
	}

	/**
	 * 根据Model的内容更新需要处理的元素内容，在产生页面时要调用
	 * 如果需要Model处理的元素，则在该方法中调用HTMLBasicModel.updateElement()
	 */
	protected void updateElement() throws Exception {
		Collection rights = ((LicenseFormModel) model).getRights();
		Element dealElement = (Element) ExtendElement.findElements(
				this.getElement(), "righttypes", "righttypes").get(0);
		ExtendElement.removeChilds(dealElement);
		Element ele = (Element) rightElement.getElementsByTagName("td").item(0);
		Iterator iter = rights.iterator();
		while (iter.hasNext()) {
			String item = (String) iter.next();
			Element e = (Element) ele.cloneNode(true);
			dealElement.appendChild(e);
			((LicenseFormModel) model).setRightElement(e, item, this.getPage()
					.getDocument());
		}
		if (rights.size() == 0) {
			String str = getNoRightTypesStr();
			dealElement.appendChild(ele.cloneNode(false).appendChild(
					this.getPage().getDocument().createTextNode(str)));
		}
		super.updateElement();
	}

	public String getNoRightTypesStr() {
		if (this.getElement().hasAttribute("en")) {
			return "No";
		} else {
			return "无";
		}
	}
}